<?php
defined('IN_TS') or die('Access Denied.');

/* 
 * 小组话题内容页
 */

$topicid = intval($_GET['topicid']);
$strTopic = $new['group']->getOneTopic($topicid);
	
//帖子分类
if($strTopic['typeid'] != '0'){
	$strTopic['type'] = $DB->once_fetch_assoc("select * from ".dbprefix."app_group_topics_type where typeid='".$strTopic['typeid']."'");
}

//小组
$strGroup = $DB->once_fetch_assoc("select * from ".dbprefix."app_group where groupid='".$strTopic['groupid']."'");

//判断会员是否加入该小组
$groupid = intval($strGroup['groupid']);
$userid = $TS_USER['user']['userid'];

$isGroupUser = $DB->once_num_rows("select * from ".dbprefix."app_group_users where userid='$userid' and groupid='$groupid'");

//浏览方式
if($strGroup['isopen']=='1' && $isGroupUser=='0'){

	header("Location: ".$TS_URL['group'].$strGroup['groupid'].$TS_URL['suffix']."");
	
}else{
	
	//帖子标签
	$strTopic['tags'] = aac('tag',$DB)->getObjTagByObjid('topic','topicid',$topicid);

	$strTopic['content'] = hview($strTopic['content']);
	
	//匹配本地图片
	preg_match_all('/\[(photo)=(\d+)\]/is', $strTopic['content'], $photos);
	
	foreach ($photos[2] as $item) {
		$strTopic['content'] = str_replace("[photo={$item}]",'<a href="'.getPhotoById($item).'" class="thickbox" onclick="tb_remove();"><img class="thumbnail" src="'.getPhotoById($item).'" title="'.$strTopic['title'].$item.'" /></a>', $strTopic['content']);
	}
	
	//匹配附件
	preg_match_all('/\[(attach)=(\d+)\]/is', $strTopic['content'], $attachs);
	if($attachs[2]){
		foreach ($attachs[2] as $aitem) {
		
			$strAttach = aac('attach',$DB)->getOneAttach($aitem);
			
			if($strAttach['isattach'] == '1'){
				$strTopic['content'] = str_replace("[attach={$aitem}]",'<span class="attach_down">附件下载：<a href="index.php?app=attach&ac=ajax&ts=down&attachid='.$aitem.'">'.$strAttach["attachname"].'</a></span>', $strTopic['content']);
			}else{
				$strTopic['content'] = str_replace("[attach={$aitem}]",'', $strTopic['content']);
			}
		}
	}
	
	
	
	//匹配表情
	preg_match_all('/\[(360):(\d+)\]/is', $strTopic['content'], $expression_topic);
	foreach ($expression_topic[2] as $item) {
		$strTopic['content'] = str_replace("[360:{$item}]",'<img src="data/expression/'.$item.'.gif" />', $strTopic['content']);
	}
	
	$strTopic['content'] = ubb($strTopic['content']);
	
	$strTopic['user']	= aac('user',$DB)->getUserForApp($strTopic['userid']);
	
	$strTopic['user']['signed'] = hview($strTopic['user']['signed']);
	
	$title = $strTopic['title'];
	
	/*
	 *评论列表
	 */
	
	$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
	
	//URL
	if($TS_APP['options']['isrewrite']=='0'){
		$url = $TS_URL['topic'].$topicid.'&page=';
	}else{
		$url = $TS_URL['topic'].$topicid.'-';
	}
	
	$arrContentComment = $new['group']->getGroupContentComment($page,5,$topicid);
	
	if(is_array($arrContentComment)){
		foreach($arrContentComment as $key=>$item){
			$arrTopicComment[] = $item;
			
			//$arrTopicComment[$key]['content'] = hview(preg_replace($pattern, '<a rel="nofollow" target="_blank" href="\1\2">\1\2</a>', $item['content']));
			
			//匹配图片
			preg_match_all('/\[(photo)=(\d+)\]/is', $arrTopicComment[$key]['content'], $photos);
			if($photos[2]){
				foreach ($photos[2] as $pitem) {
					$arrTopicComment[$key]['content'] = str_replace("[photo={$pitem}]",'<img src="'.getPhotoById($pitem).'" />', $arrTopicComment[$key]['content']);
				}
			}

			//匹配表情
			preg_match_all('/\[(360):(\d+)\]/is', $arrTopicComment[$key]['content'], $expression_comment);
			foreach ($expression_comment[2] as $sitem) {
				$arrTopicComment[$key]['content'] = str_replace("[360:{$sitem}]",'<img src="data/expression/'.$sitem.'.gif" />', $arrTopicComment[$key]['content']);
			}
			
			$arrTopicComment[$key]['content'] = ubb($arrTopicComment[$key]['content']);

		}
	}
	
	$groupContentCommentNum = $new['group']->getGroupContentCommentNum('topicid',$topicid);
	
	$pageUrl = pagination($groupContentCommentNum, 5, $page, $url,$TS_URL['suffix']);
	
	//判断会员是否加入该小组
	$userid = $TS_USER['user']['userid'];
	
	$isGroupUser = $DB->once_num_rows("select * from ".dbprefix."app_group_users where userid='$userid' and groupid='".$strTopic['groupid']."'");

	//最新帖子
	$groupid = $strTopic['groupid'];
	
	//小组成员
	$strGroupUser = $DB->once_fetch_assoc("select * from ".dbprefix."app_group_users where userid='$userid' and groupid='".$strTopic['groupid']."'");
	
	$newTopic = $DB->fetch_all_assoc("select * from ".dbprefix."app_group_topics where groupid='$groupid' and isshow='0' order by addtime desc limit 6");
	
	if($page > '1'){
		$titlepage = " - 第".$page."页";
	}else{
		$titlepage='';
	}
	
	$title =$title.$titlepage.' - '.$strTopic['groupname'];
	
	include TmooTemplate("topic");
	
	$DB->query("update ".dbprefix."app_group_topics set `count_view`=count_view+1 where topicid='".$topicid."'");
	
}

function getPhotoById($photoid){
	global $DB;
	$strPhoto = $DB->once_fetch_assoc("select * from ".dbprefix."app_photo where photoid='$photoid'");
	
	return $strPhoto['photourl'];
}